﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.UserTypes;
using NHibernate.SqlTypes;
using NHibernate;
using Mulala.Cavan.Core.Extension;
using Mulala.Cavan.Domain.MainBoundedContext.Setting;

namespace Mulala.Cavan.Domain.MainBoundedContext.CorporationModule
{
    public class CorporationContact : IUserType
    {
        private static readonly SqlType[] TYPES = new SqlType[] { NHibernateUtil.String.SqlType };

        public virtual string Contact { get; set; }
        public virtual string ContactEmail { get; set; }
        public virtual string Address { get; set; }
        public virtual string Telephone { get; set; }
        public virtual string Mobile { get; set; }
        public virtual string Fax { get; set; }

        #region IUserType Members

        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

        public object DeepCopy(object value)
        {
            if (value == null)
            {
                return null;
            }
            CorporationContact sourceCorporationContact = (CorporationContact)value;
            CorporationContact targetCorporationContact = new CorporationContact();
            targetCorporationContact.Address = sourceCorporationContact.Address;
            targetCorporationContact.Contact = sourceCorporationContact.Contact;
            targetCorporationContact.ContactEmail = sourceCorporationContact.ContactEmail;
            targetCorporationContact.Fax = sourceCorporationContact.Fax;
            targetCorporationContact.Mobile = sourceCorporationContact.Mobile;
            targetCorporationContact.Telephone = sourceCorporationContact.Telephone;

            return targetCorporationContact;
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        public bool Equals(object x, object y)
        {
            if (x == y)
            {
                return true;
            }
            if (x != null && y != null)
            {
                CorporationContact xCorporationContact = (CorporationContact)x;
                CorporationContact yCorporationContact = (CorporationContact)y;
                if (xCorporationContact.Address != yCorporationContact.Address)
                {
                    return false;
                }
                if (xCorporationContact.Contact != yCorporationContact.Contact)
                {
                    return false;
                }
                if (xCorporationContact.ContactEmail != yCorporationContact.ContactEmail)
                {
                    return false;
                }
                if (xCorporationContact.Fax != yCorporationContact.Fax)
                {
                    return false;
                }
                if (xCorporationContact.Mobile != yCorporationContact.Mobile)
                {
                    return false;
                }
                if (xCorporationContact.Telephone != yCorporationContact.Telephone)
                {
                    return false;
                }
            }
            return true;
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public bool IsMutable
        {
            get { return false; }
        }

        public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
        {
            string contact = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
            string contactEmail = (string)NHibernateUtil.String.NullSafeGet(rs, names[1]);
            string address = (string)NHibernateUtil.String.NullSafeGet(rs, names[2]);
            string telephone = (string)NHibernateUtil.String.NullSafeGet(rs, names[3]);
            string mobile = (string)NHibernateUtil.String.NullSafeGet(rs, names[4]);
            string fax = (string)NHibernateUtil.String.NullSafeGet(rs, names[5]);

            CorporationContact corporationContact = new CorporationContact();
            if (contact.IsNotEmpty())
            {
                corporationContact.Contact = contact;
            }
            if (contactEmail.IsNotEmpty())
            {
                corporationContact.ContactEmail = contactEmail;
            }
            if (address.IsNotEmpty())
            {
                corporationContact.Address = address;
            }
            if (telephone.IsNotEmpty())
            {
                corporationContact.Telephone = telephone;
            }
            if (mobile.IsNotEmpty())
            {
                corporationContact.Mobile = mobile;
            }
            if (fax.IsNotEmpty())
            {
                corporationContact.Fax = fax;
            }
            return corporationContact;
        }

        public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
        {
            if (value != null)
            {
                CorporationContact corporationContact = (CorporationContact)value;
                if (index == 0)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, corporationContact.Contact, index);
                }
                if (index == 1)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, corporationContact.ContactEmail, index);
                }
                if (index == 2)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, corporationContact.Address, index);
                }
                if (index == 3)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, corporationContact.Telephone, index);
                }
                if (index == 4)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, corporationContact.Mobile, index);
                }
                if (index == 5)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, corporationContact.Fax, index);
                }
            }
            else
            {
                if (index == 0)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, Project.Settings.DefaultInfo, index);
                }
                if (index == 1)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, Project.Settings.DefaultInfo, index);
                }
                if (index == 2)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, Project.Settings.DefaultInfo, index);
                }
                if (index == 3)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, Project.Settings.DefaultInfo, index);
                }
                if (index == 4)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, Project.Settings.DefaultInfo, index);
                }
                if (index == 5)
                {
                    NHibernateUtil.String.NullSafeSet(cmd, Project.Settings.DefaultInfo, index);
                }
            }
        }

        public object Replace(object original, object target, object owner)
        {
            return original;
        }

        public Type ReturnedType
        {
            get { return typeof(CorporationContact); }
        }

        public SqlType[] SqlTypes
        {
            get { return TYPES; }
        }

        #endregion
    }
}
